//
//  NSAttributedString+Extension.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/16.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

extension NSAttributedString {
//    
    class func attributedStringWithEmoticon(emoticon:WBEmoticon,font:UIFont) ->NSAttributedString {
        
        // 1. 创建UIImage对象
        let image = UIImage(named: emoticon.path!, in: WBEmoticonTools.sharedTools.emoticonBundle, compatibleWith: nil)
        
        // 2 创建文本附件(NSTextAttachment)
        let attachment = WBEmoticonTextAttachment()
        // 文本附件关联表情模型
        attachment.emoticon = emoticon
        // 获取文字高度
        let linHeight = font.lineHeight
        // 设置bounds, 设置文本附件的bounds会影响image的显示的位置
        attachment.bounds = CGRect(x: 0, y: -4, width: linHeight, height: linHeight)
        // 3. 设置文本附件对应的图片
        attachment.image = image
        // 4. 根据文本附件创建富文本
        let emoticonAttributeText = NSAttributedString(attachment: attachment)

        return emoticonAttributeText
    }
    
    
}
